package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.domain.FileInfo;
import com.authine.cloudpivot.web.api.secondopen.modules.AdvertVO;
import com.authine.cloudpivot.web.api.secondopen.service.impl.AdvertServiceImpl;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * 悬浮图片
 */
@RestController
@RequestMapping("/api/advert")
public class AdvertController {
    private static final Logger log = LoggerFactory.getLogger(AdvertController.class);

    @Autowired
    private AdvertServiceImpl advertService;

    @GetMapping("/getadvert")
    public AjaxResult getAdvert() {
        List<AdvertVO> advert = advertService.getAdvert();
        List<FileInfo> list = new ArrayList<>();
        for (int i = 0; i < advert.size(); i++) {
            AdvertVO advertVO = advert.get(i);
            String id = advertVO.getId();
            String title = advertVO.getTitle();
            FileInfo photo = advertService.getPhoto(id);
            photo.setId(id);
            photo.setDesc(title);
            String refid = photo.getRefid();
            HttpGet httpGet = new HttpGet("http://192.9.200.117:5600/api/api/aliyun/download" + "?refId=" + refid);
            InputStream in = null;
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                RequestConfig timeoutconfig = RequestConfig.custom().setConnectTimeout(5000).
                        setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();
                httpGet.setConfig(timeoutconfig);
                HttpResponse response = httpClient.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statuscode = statusLine.getStatusCode();
                if (statuscode == 200) {
                    HttpEntity httpEntity = response.getEntity();
                    in = httpEntity.getContent();
                    byte[] bytes = IOUtils.toByteArray(in);
                    String s = Base64.getEncoder().encodeToString(bytes);
                    photo.setImage(s);
                }
            } catch (Exception e) {
                log.info("获取文件失败");
                e.printStackTrace();
            }
            list.add(photo);
        }
        return AjaxResult.success(list);
    }
}
